//
//  TripOverViewViewModel.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/9/28.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

enum TripOverViewCellType {
    case card
    case city
    case ad
}

class TripOverViewViewModel: NSObject {

    var trip:Trip?
    
    var loadFinished:((String?)-> Void)?
    var loadBegining:((Bool) ->Void)?
    
    var tripid:String?
    
    var cards:[Calendar] {
        
        var array: [Calendar] = []
        
        if let airplanes = airplanes {
            
            array += airplanes
        }
        
        if let hotels = hotels {
            
            array += hotels
        }
        
        return array
    }
    
    var hotels:[Calendar]? {
        
        guard let cities = trip?.cities else {
            
            return nil
        }
        
        let hotels = cities.reduce([]) { (array, city) -> [Calendar] in
            
            
            if let hotels = city.hotels {
                
                return array + hotels
            }
            
            return array
        }
        
        return hotels
    }
    
    var airplanes:[Calendar]? {
        
        return trip?.flights?.compactMap({ airplan -> Calendar? in

            let cal = Calendar()
            cal.airplaneDetail = airplan
            cal.planType = .flight

            return cal
        })
        
    }
    
    var featureCities:[City]? {
        
        if let cities = trip?.cities?.compactMap({ city -> City? in
            
            if city.featureCalendar?.count ?? 0 > 0 {
                
                return city
            }
            
            return nil
        }) {
            
            return cities
        }
        
        return nil
    }
    
    var advertisements:[TripAD]? {
        
        return trip?.advertisements
    }
    
    var cellType:[TripOverViewCellType]? {
        
        var cells:[TripOverViewCellType] = []
        
        if cards.count > 0 {
            
            cells.append(.card)
        }
        
        if featureCities?.count ?? 0 > 0 {
            
            cells.append(.city)
        }
        
        if advertisements?.count ?? 0 > 0 {
            
            cells.append(.ad)
        }

        return cells
    }
    
    
    func reloadData() {
        
        guard let bestid = tripid else {
            
            loadFinished?(LocalizedString("analysis_error"))
            return
        }
        
        loadBegining?(true)
        NetAPI.tripdetail(tripid: bestid).request(Trip.self) {
            
            [weak self] response, errmsg in
            guard let `self` = self else { return }
            
            if let trip = response as? Trip {

                self.trip = trip
                TripManager.share.lasttrip = trip
                TripManager.share.lasttripid = trip.orderId
            }
            self.loadFinished?(errmsg)
            self.loadBegining?(false)
        }
    }
}
